package com.andaily.web.controller.mobile;

import com.andaily.domain.shared.DateUtils;
import com.andaily.infrastructure.support.LogHelper;
import com.andaily.service.AccountService;
import com.andaily.service.LogService;
import com.andaily.service.ThingService;
import com.andaily.web.util.CookieUserAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Shengzhao Li
 */
@Controller
public class MainController {

    private static LogHelper log = LogHelper.create(MainController.class);

    private AccountService accountService;
    @Autowired
    private LogService logService;
    @Autowired
    private ThingService thingService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request, ModelMap model) throws Exception {
        log.info("Go to mobile login...");
        CookieUserAssistant cookieUserAssistant = new CookieUserAssistant(request).retrieve();
        model.addAttribute("username", cookieUserAssistant.getUsername());
        model.addAttribute("password", cookieUserAssistant.getPassword());

        return "mobile_login";
    }


    @RequestMapping("/dashboard")
    public String dashboard(ModelMap model) throws Exception {
        model.addAttribute("accountTotal", accountService.loadTotalAccount());
        model.addAttribute("logTotal", logService.loadTotalLog());
        model.addAttribute("thingTotal", thingService.loadTotalThing());
        //current year
        model.addAttribute("year", DateUtils.currentYear());
        return "mobile_dashboard";
    }

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
}
